import { Service } from 'egg';
import { User } from '../model/User';

interface tokenType {
  account: string;
  password: string;
  type: number;
}
// interface searchInfoType{
//   account: string;
//   username: string;
//   sex: string;
//   type: null | number;
//   pageSize: number;
//   current: number;
// }
export default class UserService extends Service {
  /**
   * 查询所有用户
   * @return 用户信息
   */
  public async queryUserAll(searchInfo, decode:tokenType):Promise<object> {
    if (decode.type !== 3) {
      return {
        code: 1,
        message: '该用户无权限操作',
        data: [],
      };
    }
    const result = await this.app.mysql.beginTransactionScope(async conn => {
      const sql = `SELECT * FROM users WHERE
                account LIKE '%${searchInfo.account.trim()}%' AND username LIKE '%${searchInfo.username.trim()}%' 
                AND sex LIKE '%${searchInfo.sex}%' AND type LIKE '%${searchInfo.type}%' LIMIT ${(searchInfo.current - 1) * searchInfo.pageSize}, ${searchInfo.pageSize}`;
      const userList = await conn.query(sql);
      const pageSize = await conn.query(`select count(*) as count from users WHERE account LIKE '%${searchInfo.account}%' AND username LIKE '%${searchInfo.username}%' AND sex LIKE '%${searchInfo.sex}%' AND type LIKE '%${searchInfo.type}%'`);
      return {
        code: 0,
        message: '操作成功',
        data: { userList, total: pageSize[0].count },
      };
    }, this.ctx);
    return result;
  }
  /**
   * 删除用户
   * @param id 用户id
   */
  public async delUser(id:number, decode:tokenType) {
    if (decode.type !== 3) {
      return {
        code: 1,
        message: '该用户无权限操作',
        data: [],
      };
    }
    const result:object = await this.app.mysql.beginTransactionScope(async conn => {
      const user:User[] = await conn.select('users', { where: { id } }) as User[];
      if (user.length === 0) {
        return {
          code: 1,
          message: '该用户不存在',
          data: [],
        };
      }
      const isAdmin = await conn.select('users', { where: { account: decode.account, password: decode.password } }) as User[];
      if (isAdmin[0]?.id === id) {
        return {
          code: 1,
          message: '您不可以删除自己',
          data: [],
        };
      }
      await conn.delete('users', { id });
      return {
        code: 0,
        message: '删除成功',
        data: [],
      };
    }, this.ctx);
    return result;
  }

  /**
   * 修改用户
   * @param id 教师id
   * @param data 用户信息
   * @param decode token
   */
  public async updateUser(id:number, data:User, decode: tokenType):Promise<object> {
    if (decode.type !== 3) {
      return {
        code: 1,
        message: '该用户无权限操作',
        data: [],
      };
    }
    const result = await this.app.mysql.beginTransactionScope(async conn => {
      const user:Array<User> = await conn.select('users', { id }) as Array<User>;
      if (user.length === 0) {
        return {
          code: 1,
          message: '该用户不存在',
          data: [],
        };
      }
      data.username.trim();
      data.created_time = new Date();
      data.updated_time = new Date();
      await conn.update('users', data, { id });
      return {
        code: 0,
        message: '修改成功',
        data: [],
      };
    }, this.ctx);
    return result;
  }
}
